#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

# 下载设置
chrome_options = Options()
prefs = {'profile.default_content_settings.popups': 0,
         'download.default_directory': os.getcwd()}


chrome_options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(options=chrome_options)

driver.get("https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E6%B7%B1%E5%9C%B3")

#定位图片元素
images = driver.find_elements_by_css_selector('div.imgbox')

count = 0
for img in images:
    ActionChains(driver).move_to_element(img).perform()
    time.sleep(0.5)
    pica = driver.find_element_by_css_selector('div.hover a.down')
    pica.click()

    # 只下载前五张图片
    count += 1
    if count >= 5:
        break
